package com.fc.test.service;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fc.test.mapper.auto.EntInfoMapper;
import com.fc.test.mapper.auto.EntJobMapper;
import com.fc.test.mapper.auto.StuCvMapper;
import com.fc.test.model.auto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.StuPostRecordMapper;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 * 学生投递信息表 StuPostRecordService
 * @Title: StuPostRecordService.java 
 * @Package com.fc.test.service 
 * @author DrRic_自动生成
 * @email drhyz@qq.com
 * @date 2020-07-05 17:21:02  
 **/
@Service
public class StuPostRecordService implements BaseService<StuPostRecord, StuPostRecordExample>{
	@Autowired
	private StuPostRecordMapper stuPostRecordMapper;
	@Autowired
	private EntInfoMapper entInfoMapper;
	@Autowired
	private StuCvMapper stuCvMapper;
	@Autowired
	private EntJobMapper entJobMapper;
      	   	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Object> list(Tablepar tablepar,StuPostRecord record){
		 	StuPostRecordExample testExample=new StuPostRecordExample();
	 		if(record.getSid()!=null){
				testExample.createCriteria().andSidEqualTo(record.getSid());
				testExample.setOrderByClause("post_id ASC");
				if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
					testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
				}
				PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
				List<StuPostRecord> list= stuPostRecordMapper.selectByExample(testExample);
				JSONArray result = new JSONArray();
				for(StuPostRecord tmp : list){
					JSONObject rec = new JSONObject();
					rec.put("StuPostRecord", tmp);
					StuCv stuCv = stuCvMapper.selectByPrimaryKey(tmp.getSid());
					EntInfo entInfo = entInfoMapper.selectByPrimaryKey(tmp.getEid());
					EntJob entJob = entJobMapper.selectByPrimaryKey(tmp.getJid());

					rec.put("StuCv", stuCv!=null?stuCv:new StuCv());
					rec.put("EntInfo", entInfo!=null?entInfo:new EntInfo());
					rec.put("EntJob", entJob!=null?entJob:new EntJob());

					result.add(rec);
				}
				PageInfo<Object> pageInfo = new PageInfo<>(result);
				return  pageInfo;
			}
//	 		else if(record.getEid()!=null){
//				testExample.createCriteria().andEidEqualTo(record.getEid());
//				testExample.setOrderByClause("post_id ASC");
//				if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
//					testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
//				}
//				PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
//				List<StuPostRecord> list= stuPostRecordMapper.selectByExample(testExample);
//				PageInfo<StuPostRecord> pageInfo = new PageInfo<StuPostRecord>(list);
//				return  pageInfo;
//			}
	 		else {
	 			return new PageInfo<>();
			}
	 }

	public PageInfo<StuPostRecord> listForSys(Tablepar tablepar, StuPostRecord record){
		StuPostRecordExample testExample=new StuPostRecordExample();
		testExample.createCriteria().andLikeQuery(record);
		testExample.setOrderByClause("post_id ASC");
		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		}
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<StuPostRecord> list= stuPostRecordMapper.selectByExample(testExample);
		PageInfo<StuPostRecord> pageInfo = new PageInfo<StuPostRecord>(list);
		return  pageInfo;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			StuPostRecordExample example=new StuPostRecordExample();
			example.createCriteria().andPostIdIn(stringB);
			return stuPostRecordMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public StuPostRecord selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return stuPostRecordMapper.selectByPrimaryKey(id1);
				
	}

	public Object findProcessStuCv(String id) {

		Integer id1 = Integer.valueOf(id);
		StuPostRecord stuPostRecord = stuPostRecordMapper.selectByPrimaryKey(id1);
		EntInfo entInfo = entInfoMapper.selectByPrimaryKey(stuPostRecord.getEid());
		JSONObject result = new JSONObject();
		result.put("StuPostRecord", stuPostRecord);
		result.put("EntInfo", entInfo);

		return result;
	}

	public List<StuPostRecord> selectByEid(String id) {
	 		Integer id1 = Integer.valueOf(id);
			return stuPostRecordMapper.selectByEid(id1);
	}

	
	@Override
	public int updateByPrimaryKeySelective(StuPostRecord record) {
		return stuPostRecordMapper.updateByPrimaryKeySelective(record);
	}

	public int updateStatusBySid(String id, Integer status) {
		Integer id1 = Integer.valueOf(id);
		return stuPostRecordMapper.updateStatusBySid(id1, status);
	}
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(StuPostRecord record) {
				
		record.setPostId(null);
		
				
		return stuPostRecordMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(StuPostRecord record, StuPostRecordExample example) {
		
		return stuPostRecordMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(StuPostRecord record, StuPostRecordExample example) {
		
		return stuPostRecordMapper.updateByExample(record, example);
	}

	@Override
	public List<StuPostRecord> selectByExample(StuPostRecordExample example) {
		
		return stuPostRecordMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(StuPostRecordExample example) {
		
		return stuPostRecordMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(StuPostRecordExample example) {
		
		return stuPostRecordMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param stuPostRecord
	 * @return
	 */
	public int checkNameUnique(StuPostRecord stuPostRecord){
		StuPostRecordExample example=new StuPostRecordExample();
		example.createCriteria().andStatusEqualTo(stuPostRecord.getStatus());
		List<StuPostRecord> list=stuPostRecordMapper.selectByExample(example);
		return list.size();
	}


}
